<template>
  <div class="login-container">
    <el-form
      v-if="isActive"
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <p class="title" style="color: #414042">开始精彩活动之旅</p>
      </div>
      <el-form-item prop="mobile">
        <el-input
          ref="username"
          v-model="loginForm.mobile"
          placeholder="请输入手机号"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        ></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        >
        </el-input>
      </el-form-item>
      <el-link
        style="margin-bottom: 20px"
        :underline="false"
        key=""
        :class="isActiveIndex === 1 ? 'isActiveTitle' : ''"
        @click="smsLogin"
      >
        短信验证码登录
      </el-link>
      <el-button
        class="loginBtn"
        :loading="loading"
        type="primary"
        style="width: 85%; margin-bottom: 30px; border-radius: 10px"
        @click.native.prevent="handleLogin"
      >
        登录
      </el-button>
      <br />
      <el-link :underline="false">注册账号</el-link>
      <el-link :underline="false" style="margin-left: 320px">找回密码</el-link>
    </el-form>
    <!-- 短信验证的登录 -->
    <el-form
      v-else
      ref="loginForm2"
      :model="loginForm2"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <p class="title" style="color: #414042">开始精彩活动之旅</p>
      </div>
      <el-form-item prop="mobile">
        <el-input
          ref="username"
          v-model="loginForm2.mobile"
          placeholder="请输入手机号"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
          suffix-icon="el-icon-phone"
        >
          <el-select v-model="select" slot="prepend" placeholder="请选择地区">
            <el-option label="中国 +86" value="1"></el-option>
            <el-option label="美国 001" value="2"></el-option>
            <el-option label="英国 0044" value="3"></el-option>
            <el-option label="加拿大 0001" value="4"></el-option>
          </el-select>
        </el-input>
      </el-form-item>

      <el-form-item prop="verifyCode">
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm2.verifyCode"
          :type="passwordType"
          placeholder="请输入验证码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        >
          <el-button slot="append">获取验证码</el-button>
        </el-input>
      </el-form-item>
      <el-link
        style="margin-bottom: 20px"
        :underline="false"
        :class="isActiveIndex === 0 ? 'isActiveTitle' : ''"
        @click="accountLogin"
      >
        账号密码登录
      </el-link>
      <el-button
        class="loginBtn"
        :loading="loading"
        type="primary"
        style="width: 85%; margin-bottom: 30px; border-radius: 10px"
        @click.native.prevent="handleLogin"
      >
        登录
      </el-button>
      <br />
      <el-link :underline="false">注册账号</el-link>
      <el-link :underline="false" style="margin-left: 320px">找回密码</el-link>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login-center",
  data () {
    const validateMobile = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("手机号不能为空"))
      }
      if (!/^1[3-9]\d{9}$/.test(value)) {
        return callback(new Error("手机号格式不对"))
      }
      callback()
    }
    return {
      select: '',
      isActive: true,
      isActiveindex: 0,
      loginForm: {
        mobile: "13800000002",
        password: "123456"
      },
      loginForm2: {
        mobile: "",
        verifyCode: ""
      },
      loginRules: {
        mobile: [
          {
            validator: validateMobile, trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "密码必填",
            trigger: "blur"
          },
          {
            min: 6,
            max: 16,
            message: "密码长度在6到16位之间"
          }
        ]
      },
      loading: false,
      passwordType: "password",
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    // 账号登录
    accountLogin () {
      this.isActive = true,
        this.isActiveindex = 0
    },
    // 短信登录
    smsLogin () {
      this.isActive = false,
        this.isActiveindex = 1
    },
    // 点击登录执行的方法
    handleLogin () {
      console.log(this.$refs.loginForm)
      this.$refs.loginForm.validate(
        async (valid) => {
          if (valid) {
            this.loading = true
            console.log("login")
            try {
              // await this.$store.dispatch("user/fetchLogin", this.loginForm)
              // 登录接口逻辑--->判断的登录成功跳转首页
              this.loading = false
            } catch (error) {
              this.loading = false
            }
          }
        }
      )
    }
  }
}
</script>
<style lang="scss" scoped>
$bg: #283443;
$light_gray: #68b0fe;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    // display: inline-block;
    // height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    border-radius: 5px;
    color: #454545;
  }

  .el-form-item__error {
    color: #fff;
  }
}
</style>
<style lang="scss" scoped>
// 只对当前组件生效的样式
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: rgb(10, 1, 1);

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url("~@/assets/imgs/index/bg_login.png"); // 设置背景图片
  background-position: left 10px top 15px; // 将图片位置设置为充满整个屏幕
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 100px 150px 200px;
    overflow: hidden;
  }
  .loginBtn {
    background: #f8b844;
    border: 0;
    // height: 64px;
    line-height: 32px;
    font-size: 24px;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 56px;
      color: $light_gray;
      margin: 0px auto 150px auto;
      // text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>

